package com.learner.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.learner.config.Msg;
import com.learner.entity.Department;
import com.learner.service.DepartmentService;
import com.learner.service.UserService;
import com.learner.utils.PageUtil;
import com.learner.utils.ResponseUtil;
import com.learner.utils.StringUtil;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Auther: qho
 * @Date: 2018/10/12 08:08
 * @Description:
 */
@Controller
@RequestMapping("/department")
public class DepartmentController {

    @Autowired
    private DepartmentService departmentService;

    @Autowired
    private UserService userService;

    @RequestMapping("/list")
    public ModelAndView list(@RequestParam(value = "page",defaultValue = "1")Integer page, Department s_department, HttpServletRequest request){
        ModelAndView mav = new ModelAndView();
        HttpSession session = request.getSession();
        Map<String,Object> map=new HashMap<String,Object>();
        map.put("deptName", StringUtil.formatLike(s_department.getDeptName()));
        PageHelper.startPage(page, 5,true);
        List<Department> departmentList=departmentService.find(map);
        PageInfo<Department> pageInfo = new PageInfo<>(departmentList);
        String pageCode= PageUtil.getPagation(request.getContextPath()+"/department/list.do", Integer.parseInt(String.valueOf(pageInfo.getTotal())), page, 5);
        mav.addObject("pageCode", pageCode);
        mav.addObject("modeName", "部门管理");
        mav.addObject("departmentList", departmentList);
        mav.addObject("mainPage", "department/list.jsp");
        mav.setViewName("main");
        return mav;
    }

    @RequestMapping("/preSave")
    public ModelAndView preSave(@RequestParam(value="id",required=false)String id){
        ModelAndView mav=new ModelAndView();
        mav.addObject("mainPage", "department/save.jsp");
        mav.addObject("modeName", "部门管理");
        mav.setViewName("main");
        if(StringUtil.isNotEmpty(id)){
            mav.addObject("actionName", "部门修改");
            Department department=departmentService.loadById(Integer.parseInt(id));
            mav.addObject("department", department);
        }else{
            mav.addObject("actionName", "部门添加");
        }
        return mav;
    }

    @RequestMapping("/save")
    public String save(Department department){
        if(department.getId()==null){
            departmentService.add(department);
        }else{
            departmentService.update(department);
        }
        return "redirect:/department/list.do";
    }

    @RequestMapping("/delete")
    public void delete(@RequestParam(value="id")String id, HttpServletResponse response)throws Exception{
        JSONObject result=new JSONObject();
        if(userService.existUserByDeptId(Integer.parseInt(id))){
            result.put("errorInfo", "该部门下存在用户，不能删除！");
        }else{
            departmentService.delete(Integer.parseInt(id));
            result.put("success",true);
        }
        ResponseUtil.write(result,response);
    }
}
